import Vue from 'vue'
import Vuex from 'vuex'
//引入axios
import axios from 'axios'

//引入插件
import createPersistedState from 'vuex-persistedstate'
Vue.use(Vuex)

export default new Vuex.Store({
  plugins: [createPersistedState()],
  state: {
    // token: localStorage.getItem('token') || ''

    //利用插件的方法持久化存储
    token: '',
    userInfo: {}
  },
  getters: {},
  mutations: {
    updateToken (state, token) {
      state.token = token
      //存储到本地
      localStorage.setItem('token', token)
    },
    undateUserInfo (state, info) {
      state.userInfo = info
    }
  },
  actions: {
    //会收到一个形参 叫做上下文 固定写法 mini store 青春版store
    async initUserInfo (context) {
      const { data: res } = await axios.get('/my/userinfo')
      // console.log(res)
      context.commit('undateUserInfo', res.data)
    }
  },
  modules: {}
})
